﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Service;
using S4Store.Models.TelSaleKpi;
using S4Store.Domain.TelSaleKpi;
using S4Store.Domain;

namespace S4Store.Controllers
{
    public class TelSaleKpiController : ReportBaseController
    {
        public TelSaleKpiService TelSaleKpiService { get; set; }
        public EmployeeService EmployeeService { get; set; }
        public CarService CarService { get; set; }
        public CarKpiService CarKpiService { get; set; }
        
        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            InjectReportModel(model);
            using (var t = new Transtion())
            {
                t.Inject(TelSaleKpiService);
                var data = TelSaleKpiService.GetCarBrandTelSaleDayDataQuery().Where(x => x.StoreId == Store.Id).ToList();
                model.MonthDayData = data;
                
                return View(model);
            }
        }

        public ActionResult MonthTd(int y,int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(TelSaleKpiService);
                var config = TelSaleKpiService.GetCarKpiMonthConfig(y, m, Store.Id);
                if (config == null)
                {
                    config = new Domain.TelSaleKpi.CarBrandTelSaleMonthConfig();
                    config.StoreId = Store.Id;
                    config.Month = new DateTime(y, m, 1);
                }
                return View(config);
            }
            
        }

        public ActionResult InputTelSaleKpiMonthConfig(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(TelSaleKpiService);
                var config = TelSaleKpiService.GetCarKpiMonthConfig(y, m, Store.Id);
                if (config == null )
                {
                    config = new Domain.TelSaleKpi.CarBrandTelSaleMonthConfig();
                    config.StoreId = Store.Id;
                    config.Month = new DateTime(y, m, 1);
                }
                return View(config);
            }
        }

        public JsonResult ModifyBackInputTelSaleKpiMonthConfig(List<CarBrandTelSaleMonthConfig> configs,DateTime m, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(TelSaleKpiService);
                if (configs == null)
                {
                    configs = new List<CarBrandTelSaleMonthConfig>();
                }
                var list = new List<CarBrandTelSaleMonthConfig>();
                foreach (var config in configs)
                {

                    config.Month = m;
                    config.StoreId = Store.Id;
                    if (isconfirm)
                    {
                        config.IsConfirmed = true;
                    }
                    if (config.Enble)
                    {
                        list.Add(config);
                    }
                }
                TelSaleKpiService.SubmitCarBrandTelSaleMonthConfig(m.Year, m.Month, Store.Id, list);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }   
        }

        public ActionResult CarBrandList(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                t.Inject(TelSaleKpiService);
                CarBrandListModel model = new CarBrandListModel();
                model.CarBrands = CarService.GetEnableCarBrand(Store.PinPaiId).ToList();
                model.CarBrandTelSaleMonthConfigs = TelSaleKpiService.GetCarBrandTelSaleMonthConfig(y, m, Store.Id).ToList();
                return View(model);
            }
        }

        public ActionResult InputTelSaleKpiDayData(int y, int m, int d)
        {
            InputTelSaleKpiDayDataModel model = new InputTelSaleKpiDayDataModel();
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                t.Inject(TelSaleKpiService);
                model.CarBrands = TelSaleKpiService.GetCarbrandsForStoreMonth(y, m, Store.Id).ToList();
                model.Date = new DateTime(y, m, d);
                model.DayDatas = TelSaleKpiService.GetTelSaleKpiDayDatas(y, m, d, Store.Id).ToList();
                model.IsConfirmed = false;
                if (model.DayDatas.Any(x => x.IsConfirmed))
                {
                    model.IsConfirmed = true;
                }
                return View(model);
            }
        }

        public ActionResult ModifyBackInputTelSaleKpiDayData(List<CarBrandTelSaleDayData> rows, int y, int m, int d, bool isconfirm = false)
        {
            foreach (var row in rows)
            {
                row.StoreId = Store.Id;
                if (isconfirm)
                {
                    row.IsConfirmed = true;
                }
                else
                {
                    row.IsConfirmed = false;
                }
            }
            using (var t = new Transtion())
            {
                t.Inject(TelSaleKpiService);
                TelSaleKpiService.SubmitTelSaleKpiDayDatas(y, m, d, Store.Id, rows);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }
    }
}
